package com.dts.servlet;

import com.dts.dao.GoodDao;
import com.dts.entity.Good;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Date;

@WebServlet(name = "update", urlPatterns = "/update")
public class UpdateServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String description = request.getParameter("description");
        try{
            GoodDao.updateGood(new Good(Integer.parseInt(id), name, Double.parseDouble(price), description,new Date()));
            response.sendRedirect("list");
        }catch (Exception e){
            request.setAttribute("msg", "更新失败");
            request.getRequestDispatcher("update.jsp?id="+id).forward(request, response);
        }
    }
}
